using Sirenix.OdinInspector;
using UnityEngine;
using UnityEngine.UI;
using EffectTypeValue = System.Int32;

namespace Life.ESound
{
    public class EffectSoundPlayer : MonoBehaviour
    {
        [SerializeField][EffectSelect]
        EffectTypeValue effectSoundType = 1;
        [SerializeField] bool isAutoRegistration;
        [SerializeField][ShowIf("isAutoRegistration")] Selectable selectable;
        private void Start()
        {
            if (isAutoRegistration)
            {
                if (selectable == null)
                {
                    selectable = GetComponent<Selectable>();
                }
                if (selectable is Button button)
                    button.onClick.AddListener(Play);
                else if (selectable is Toggle toggle)
                    toggle.onValueChanged.AddListener((value) => { if (value) Play(); });
            }
        }

        public void Play()
        {
            SoundController.Instance.PlayEffect(effectSoundType);
        }

#if UNITY_EDITOR
        private void Reset()
        {
            selectable = GetComponent<Selectable>();
            if (selectable != null)
            {
                isAutoRegistration = true;
            }
        }
#endif
    }
}
